﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using EbbinghausWord.Common;
using Microsoft.Phone.Tasks;

namespace EbbinghausWord.BLL
{
    /// <summary>
    /// 反馈管理
    /// </summary>
    public class FeedbackManager
    {
        /// <summary>
        /// 获得反馈的邮件标题
        /// </summary>
        private static string GetFeedbackMailTitle()
        {
            return string.Format("极速背单词{0}{1}{2}", SystemConfig.Version, new UserConfig().UserBookId, SystemConfig.AppCode);
        }

        /// <summary>
        /// 打开打开邮件反馈页面
        /// </summary>
        public static void OpenFeedbackMail()
        {
            var task = SefeTask.SafelyCreateTask<EmailComposeTask>();
            if (task == null) return;

            task.To = @"derekxu416@hotmail.com";
            task.Subject = GetFeedbackMailTitle();
            task.Show();
        }

        /// <summary>
        /// 打开评价页面
        /// </summary>
        public static void OpenRank()
        {
            var task = SefeTask.SafelyCreateTask<MarketplaceReviewTask>();

            if (task == null) return;
            //task.ContentIdentifier = SystemConfig.AppId;
            //task.ContentType = MarketplaceContentType.Applications;
            task.Show();
        }

        /// <summary>
        /// 打开市场正式版应用
        /// </summary>
        public static void OpenPaidMarkeplace()
        {
            var task = SefeTask.SafelyCreateTask<MarketplaceDetailTask>();

            if (task == null) return;
            task.ContentIdentifier = "fbb78ae3-e82c-468b-808a-3df7828e7c83";
            task.ContentType = MarketplaceContentType.Applications;
            task.Show();
        }

        /// <summary>
        /// 统计一下
        /// </summary>
        public static void CalcCount()
        {
            try
            {
                var nowString = DateTime.Now.ToEWDateFormat();
                var userConfig = new UserConfig();

                if (nowString == userConfig.LastCalcCountDate) return;
                //if (userConfig.UserBookId == 0) return;

                //拿到字符串
                var path = @"http://218.3.209.66:9999/default.aspx?b=" + userConfig.UserBookId + "&v="
                    + SystemConfig.Version + SystemConfig.AppCode;

                //提交，异步执行
                var client = new WebClient();
                client.OpenReadCompleted += (s, ex) =>
                                                      {
                                                          userConfig.LastCalcCountDate = nowString;
                                                      };
                client.OpenReadAsync(new Uri(path, UriKind.Absolute));
            }
            catch (Exception ex)
            {
            }
        }
    }
}
